// Login cache

MyApp.common.setCachedUserInfo = function (userInfo) {
  if (window.sessionStorage) {
    window.sessionStorage.setItem("MyApp.common.userInfo", Ext.encode(userInfo));
  }
};

MyApp.common.removeCachedUserInfo = function () {
  if (window.sessionStorage) {
    window.sessionStorage.removeItem("MyApp.common.userInfo");
  }
};

MyApp.common.getCachedUserInfo = function () {
  if (window.sessionStorage) {
    return MyApp.common.decodeJson(window.sessionStorage.getItem("MyApp.common.userInfo"));
  } else {
    return null;
  }
};

// Login helper object

MyApp.common.login = new function () {
  if (typeof rolesAllowed == "undefined" || rolesAllowed == null) {
    rolesAllowed = [];
  }

  var loginData = null;
  
  var Component =  Ext.extend(Ext.util.Observable, {
    constructor : function() {
      this.addEvents('loginVerified');
      Component.superclass.constructor.call(this);
    }
  });
  
  var myComponent = new Component();
  
  Ext.onReady(function () {
    var userInfo = MyApp.common.getCachedUserInfo();
    
    if(userInfo != null) {
      checkLogin(userInfo);
    } else {
      MyApp.common.sendJsonRequestAndDoOnSuccess(
        {
          url: restPath + '/js/common-login-data',
          loadIndicator: false
        },
        checkLogin
      );
    }
  });
  
  /**
   * Wait for this instead of Ext.onReady(), if you need to access the contents in the login object
   */
  function onReady(callback) {
    myComponent.on('loginVerified', callback);
  }
  this.onReady = onReady;
  
  function checkLogin(jsonResponse) {
    MyApp.common.setCachedUserInfo(jsonResponse);
    
    loginData = jsonResponse.data;
    
    if (rolesAllowed.length > 0) {
      if(loginData.loggedIn == false) {
        document.location.replace(htmlPath + "/in-page/logout/logout-confirmation.html");
        return;
      }
      
      var hasRequiredRole = false;
      for(rolesAllowedCounter = 0;  hasRequiredRole == false && rolesAllowedCounter < rolesAllowed.length; rolesAllowedCounter++) {
        hasRequiredRole = isUserInRole(rolesAllowed[rolesAllowedCounter]);
      }
      
      if(hasRequiredRole == false) {
        var requiredRolesString = "";
        for(rolesAllowedCounter = 0; rolesAllowedCounter < rolesAllowed.length; rolesAllowedCounter++) {
          if(requiredRolesString != "") {
            requiredRolesString += ", ";
          }
          requiredRolesString += rolesAllowed[rolesAllowedCounter];
        }
        
        document.location.replace(htmlPath + "/missing-role/missing-role.html");
        return;
      }
    }

    myComponent.fireEvent('loginVerified');
  }
  
  function isUserInRole(roleToCheck) {
    return typeof loginData.hasRoles != "undefined" && Ext.Array.indexOf(loginData.hasRoles, roleToCheck) > -1;
  }
  this.isUserInRole = isUserInRole;
  
  function getLoginData() {
    return loginData;
  }
  this.getLoginData = getLoginData;
}();